﻿/*-------------------------------------------------------------------------
* 命名空间: XrayDirection.ViewModels/ErrorShowViewModel
* 类       名: ErrorShowViewModel
* 功      能:  TD
* 时      间:  2023/12/26 16:53:01
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using XrayDirection.Dialogs;
using XrayDirection.Extensions;
using XrayDirection.HelperManager;
using XrayDirection.Services;

namespace XrayDirection.ViewModels
{
    public class ErrorShowViewModel : BindableBase, IDialogAware
    {
        public ErrorShowViewModel(IDialogService dialogService)
        {
            // 后面的参数表示,这个指令是否可以生效,只有当异常的个数大于0的时候,这个指令才会生效
            CopyToClipboardCommand = new DelegateCommand(OnCopyToClipboard, CanExecuteCopyToClipboard).ObservesProperty(() => Exceptions);
            this.dialogService = dialogService;
            ViewDetailsCommand = new DelegateCommand(OnViewDetails);

            OpenErrorLogDirCommand = new DelegateCommand(OnOpenErrorLogDir);
        }



        /// <summary>
        /// 打开异常错误目录,并且直接选中最新的文件.
        /// </summary>
        public DelegateCommand OpenErrorLogDirCommand { get; private set; }
        private void OnOpenErrorLogDir()
        {
            FileDirHelper.OpenDirAndSelected(Path.Combine(ServiceProvider.LogDir!, "Crashes"));
        }


        public DelegateCommand ViewDetailsCommand { get; private set; }
        private void OnViewDetails()
        {
            dialogService.ShowDialog(nameof(ErrorDetailDialog), null, result => { });
        }


        /// <summary>
        ///  复制到粘贴板的时候调用的方法
        /// </summary>
        public DelegateCommand CopyToClipboardCommand { get; private set; }

        private void OnCopyToClipboard()
        {
            var sb = new StringBuilder();
            sb.Append(SystemInfo.GetInfo());
            sb.AppendLine();
            sb.Append(Exceptions[0]);
            sb.ToString().WriteToClipboard();
        }
        private bool CanExecuteCopyToClipboard()
        {
            return Exceptions.Count > 0;
        }
        // 异常信息列表,为什么需要这个异常信息列表,因为一个异常可能包含其他的异常(内部异常等)
        public ObservableCollection<Exception> Exceptions { get; } = new ObservableCollection<Exception>();

        private Exception? _selectedException;

        public Exception? SelectedException
        {
            get { return _selectedException; }
            set
            {
                _selectedException = value;
                RaisePropertyChanged();
            }
        }


        public string Title => "异常错误提示框";

        public event Action<IDialogResult>? RequestClose;

        // 异常窗口显示的异常对象
        private readonly Exception? _exception;
        private readonly IDialogService dialogService;

        // 异常窗口显示的异常信息(在异常对象里其实就是Exception.Message)
        private string? message;

        public string? Message
        {
            get { return message; }
            set
            {
                message = value;
                RaisePropertyChanged();
            }
        }

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {

        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            Exception? exception = parameters.GetValue<Exception>("Exception");
            string? message = parameters.GetValue<string>("Message");
            if (message != null)
            {
                Message = message;
            }

            while (exception != null)
            {
                Exceptions.Add(exception);
                exception = exception.InnerException;
            }

            SelectedException = Exceptions[0];
        }
    }
}
